package com.hgyc.mom.base.controller;

import java.util.*;

import com.hgyc.mom.base.vo.MaterialImportVO;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.base.entity.Material;
import com.hgyc.mom.base.vo.MaterialParam;
import com.hgyc.mom.base.service.MaterialService;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 物料基础信息Controller
 *
 * @author fwj
 * @since 2025-04-28
 */
@Tag(name = "物料基础信息控制器", description = "物料基础信息控制器")
@RestController
@RequestMapping("/base/material")
public class MaterialController
{
    @Autowired
    private MaterialService materialService;

    @Operation(summary = "查询物料基础信息列表", description = "查询物料基础信息列表")
    @PostMapping("/pageList")
    public Result<IPage<Material>> pageList(@Parameter(description = "查询参数") @RequestBody MaterialParam materialParam)
    {
        Page<Material> page = new Page<>(materialParam.getPage(), materialParam.getPageSize());
        Material material = new Material();
        BeanUtils.copyProperties(materialParam, material);
        QueryWrapper<Material> wrapper = QueryWrapperUtil.build(material);
        IPage<Material> list = materialService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<MaterialImportVO> util = new ExcelUtil<>(MaterialImportVO.class);
        util.importTemplateExcel(response, "物料导入模板" );
    }

    @Operation(summary = "导出物料基础信息列表", description = "导出物料基础信息列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialParam materialParam)
    {
        Material material = new Material();
        BeanUtils.copyProperties(materialParam, material);
        QueryWrapper<Material> wrapper = QueryWrapperUtil.build(material);
        List<Material> list = materialService.list(wrapper);
        ExcelUtil<Material> util = new ExcelUtil<>(Material.class);
        util.exportExcel(response, list, "物料基础信息数据");
    }

    @Operation(summary = "导入物料基础信息列表", description = "导入物料基础信息列表")
    @PostMapping("/import")
    public Result importData(@RequestParam("file") MultipartFile file) throws Exception
    {
        ExcelUtil<MaterialImportVO> util = new ExcelUtil<>(MaterialImportVO.class);
        List<MaterialImportVO> materialList = util.importExcel(file.getInputStream());
        String message = materialService.importData(materialList);
        return Result.success(message);
    }

    @Operation(summary = "获取物料基础信息详细信息", description = "获取物料基础信息详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "物料基础信息ID") @PathVariable("id") Long id)
    {
        return Result.success(materialService.getById(id));
    }

    @Operation(summary = "新增物料基础信息", description = "新增物料基础信息")
    @PostMapping
    public Result save(@Parameter(description = "物料基础信息") @RequestBody Material material)
    {
        return Result.success(materialService.save(material));
    }

    @Operation(summary = "修改物料基础信息", description = "修改物料基础信息")
    @PutMapping
    public Result update(@Parameter(description = "物料基础信息") @RequestBody Material material)
    {
        return Result.success(materialService.updateById(material));
    }

    @Operation(summary = "删除物料基础信息", description = "删除物料基础信息")
    @DeleteMapping
    public Result remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids)
    {
        return Result.success(materialService.removeByIds(Arrays.asList(ids)));
    }

}
